﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Univar
{
	public class InFileCacheStore : InFileCacheStore<object>
	{
		public InFileCacheStore(string key) : base(key) { }
		public InFileCacheStore(string key, Scopes scope) : base(key, scope) { }
		public InFileCacheStore(string key, Scopes scope, TimeSpan lifeTime, bool isCompressed, bool isEncrypted)
			: base(null, key, scope, lifeTime, isCompressed, isEncrypted) { }
		public InFileCacheStore(object defaultValue, string key, Scopes scope, TimeSpan lifeTime, bool isCompressed, bool isEncrypted)
			: base(defaultValue, key, scope, lifeTime, isCompressed, isEncrypted) { }
	}

	public class InFileCacheStore<T> : StoreBase<T, InFileCacheStore<T>>
	{
		public string FolderPath { get; set; } // This can be used to override the default folder path.
		public TimeSpan? LifeTime { get; set; }
		public bool IsEncrypted { get; set; }
		public bool IsCompressed { get; set; } // TODO: Implement compression 

		public InFileCacheStore(string key) : base(key) { }
		public InFileCacheStore(string key, Scopes scope) : base(key, scope) { }
		public InFileCacheStore(string key, Scopes scope, TimeSpan lifeTime, bool isCompressed, bool isEncrypted)
			: base(default(T), key, scope) { }
		public InFileCacheStore(T defaultValue, string key, Scopes scope, TimeSpan lifeTime, bool isCompressed, bool isEncrypted)
			: base(defaultValue, key, scope)
		{
			LifeTime = lifeTime;
			IsCompressed = isCompressed; // TODO: Implement compression 
			IsEncrypted = isEncrypted;
		}

		protected override T GetValue(string key)
		{
			object value = Storage.InFileCache.Get<object>(key, IsEncrypted);
			return value == null ? DefaultValue : (T)value;
		}

		protected override void SetValue(string key, T value)
		{
			Storage.InFileCache.Set<T>(FolderPath, key, value, LifeTime, IsEncrypted, ThrowExceptionOnDeserializationOrDecryptionError);
		}

		protected override object PlainValue
		{
			get { return Storage.InFileCache.Get(Key); }
			set { Storage.InFileCache.Set(Key, value); }
		}

		/// <summary>
		/// Returns the length of the serialized value.
		/// </summary>
		/// <returns></returns>
		public new long GetSize() { return base.GetSize(); }
	}

}
